package com.espirit.common;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.hibernate.annotations.Index;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Table;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

import com.espirit.eap.annotation.DomainModel;
import com.espirit.eap.manager.EnumUserType;
import com.espirit.eap.manager.MessageViewType;
import com.espirit.eap.manager.workflow.WFInstance;
import com.espirit.eap.sa.LoginUser;
import com.espirit.eap.sa.User;
import com.googlecode.cswish.annotation.ChildClass;
import com.googlecode.cswish.model.Saas;

@Entity
@DomainModel
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "messageType", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("Message")
@Table(indexes={@Index(name="saas_idx_messageType", columnNames={"messageType"} ),
		@Index(name="saas_idx_toAndHiddenAndCreatedOn", columnNames={"to_id","hidden","createdOn"} )}, appliesTo = "saas_message")

@TypeDefs(@TypeDef(name="messageViewType" ,typeClass=EnumUserType.class,
		parameters={@Parameter(name  = "enumClassName" , value  = "com.espirit.eap.manager.MessageViewType")}))

@ChildClass({"com.espirit.common.PPMessage", "com.espirit.common.WFMessage"})
public class Message implements Serializable,VersionAware , Saas{
	private static final long serialVersionUID = -1462939642137897815L;

	private Integer id;
	private LoginUser from;
	private LoginUser to;
	private String title;
	private Boolean hidden;
	private String description;
	
	private Integer sid;
	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}
	
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@ManyToOne(targetEntity=LoginUser.class,fetch=FetchType.LAZY)
	public LoginUser getFrom() {
		return from;
	}

	public void setFrom(LoginUser from) {
		this.from = from;
	}

	@ManyToOne(targetEntity=LoginUser.class,fetch=FetchType.LAZY)
	public LoginUser getTo() {
		return to;
	}

	public void setTo(LoginUser to) {
		this.to = to;
	}

	public Boolean getHidden() {
		return hidden;
	}

	public void setHidden(Boolean hidden) {
		this.hidden = hidden;
	}

	

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(length=2000)
	public String getTitle() {
		return title;
	}

	
	public void setDescription(String description) {
		this.description = description;
	}

	@Column(length=5000)
	public String getDescription() {
		return description;
	}
	
	
	
//	private Event event;
	private WFInstance instance;
	
//	@ManyToOne(targetEntity=Event.class,fetch=FetchType.EAGER)
//	public Event getEvent() {
//		return event;
//	}
//
//	public void setEvent(Event event) {
//		this.event = event;
//	}

	public void setInstance(WFInstance instance) {
		this.instance = instance;
	}

	@ManyToOne(targetEntity=WFInstance.class,fetch=FetchType.LAZY ,cascade= CascadeType.ALL )
	public WFInstance getInstance() {
		return instance;
	}
	
	@Transient
//	@QLFormula(value="createdOn", type=Date.class)
	public String getInitDate(){
		String detailDataFormat = "yyyy-MM-dd HH:mm:ss";
		String sDate = null;
		if (this.createdOn != null) {
			java.util.Date date = this.getCreatedOn();
			if (date != null) {
				sDate = new SimpleDateFormat(detailDataFormat).format(date);
			}
		}
		if (sDate == null){
			sDate = "";
		}
		return sDate;
	}
	
	



	private MessageViewType messageViewType;

	
	@Type(type="messageViewType")
	public MessageViewType getMessageViewType() {
		return messageViewType;
	}
	
	public void setMessageViewType(MessageViewType messageViewType) {
		this.messageViewType = messageViewType;
	}
	
	private Date createdOn;
	private User created;
	
	public Date getCreatedOn() {
		return createdOn;
	}
	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}
	@ManyToOne(targetEntity = User.class,fetch=FetchType.LAZY)
	public User getCreated() {
		return created;
	}
	public void setCreated(User created) {
		this.created = created;
	}

	
//	@Transient
//	public String getI18nKey(){
//		return null;
//	}
//	@Transient
//	public String getUrl(){
//		return null;
//	}
//	
//	@Transient
//	public String getOrderNbr(){
//		return null;
//	}
	
}
